package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Structure;
import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.User;
import ge.telasi.tasks.model.validation.TaskValidator;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class NewTaskSubjectAndBodyPanel extends JPanel implements Loggable {
    private static final long serialVersionUID = 1433794774850787706L;

    public NewTaskSubjectAndBodyPanel() {
        initComponents();

        // set default structure
        User user = Platform.getDefault().getUser();
        if (user != null) {
            cmbStructure.setStructure(user.getStructure());
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtSubject = new ge.telasi.tasks.ui.comp.SuggestionText();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtBody = new ge.telasi.tasks.ui.comp.SuggestionTextArea();
        cmbStructure = new ge.telasi.tasks.ui.structure.StructureSelector();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtCancNumber = new ge.telasi.tasks.ui.comp.SuggestionText();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/email_edit.png"))); // NOI18N
        jLabel1.setText("სათაური:");

        txtSubject.setName("Task.subject"); // NOI18N
        txtSubject.setSuggestion("");

        txtBody.setColumns(20);
        txtBody.setLineWrap(true);
        txtBody.setRows(5);
        txtBody.setWrapStyleWord(true);
        txtBody.setName("Task.body"); // NOI18N
        txtBody.setSuggestion("ჩაწერეთ განმარტებით კომნეტარები");
        jScrollPane1.setViewportView(txtBody);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/application_cascade.png"))); // NOI18N
        jLabel2.setText("გამცემი სტრუქტურული ერთეული:");

        jLabel3.setText("კანც. №");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSubject, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCancNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbStructure, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(cmbStructure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCancNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(16, 16, 16)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ge.telasi.tasks.ui.structure.StructureSelector cmbStructure;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private ge.telasi.tasks.ui.comp.SuggestionTextArea txtBody;
    private ge.telasi.tasks.ui.comp.SuggestionText txtCancNumber;
    private ge.telasi.tasks.ui.comp.SuggestionText txtSubject;
    // End of variables declaration//GEN-END:variables

    private Logger logger;

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public JComponent[] allFields() {
        return new JComponent[] { txtSubject, txtBody };
    }

    protected void clearLogger() {
        LoggerUtils.clearLogger(logger, allFields());
    }

    protected boolean validateForm() {
        clearLogger();
        try {
            Task task = new Task();
            fillTask(task);
            new TaskValidator().validateSubjectAndBody(task);
            return true;
        } catch (Exception ex) {
            LoggerUtils.manageException(this, logger, ex, allFields());
            return false;
        }
    }

    public void fillTask(Task task) {
        task.setSubject(txtSubject.getText());
        task.setLastBody(txtBody.getText());
        task.setStructure(cmbStructure.getStructure());
        task.setCancelariaNumber(txtCancNumber.getText());
    }

    public void setSubject(String subject) {
        txtSubject.setText(subject);
    }

    public void setBody(String body) {
        txtBody.setText(body);
    }

    public void setStructure(Structure structure) {
        cmbStructure.setStructure(structure);
    }

}
